<script setup>
import { ref } from 'vue';

import { finishAttentionService } from '@/api/attention'

import bus from '@/utils/bus';


const isShow = ref(false);

const disabled = ref(false)
const formModel = ref({
    deviceCode: ''
})
const formRef = ref()

const rules = ref({
    deviceCode: [
        { required: true, message: '请输入设备ID', trigger: 'blur' },
    ],
})

const handleSumbit = async () => {
    await formRef.value.validate()
    disabled.value = true
    try {
        const fd = new FormData()
        fd.append('deviceCode', formModel.value.deviceCode)
        await finishAttentionService(noteId.value, fd)
        ElMessage({
            type: 'success',
            message: '任务完成，等待接收尾款'
        })
        // 借用 添加宠物的接口使用，刷新列表
        bus.emit('attention')
    } catch (err) {
        console.error('报错：', err);
        return;
    } finally {
        disabled.value = false
        close()
    }


}
const noteId = ref('')
const open = (id) => {
    // console.log('接收到的宠物id:', id);
    noteId.value = id
    isShow.value = true;
}

const close = () => {
    isShow.value = false
    formModel.value.deviceCode = ''
};

defineExpose({
    open
})

</script>
<template>
    <el-dialog v-model="isShow" title="输入设备" width="500">
        <el-form :model="formModel" :rules="rules" ref="formRef" label-width="100px">
            <el-form-item label="设备ID" prop="deviceCode">
                <el-input v-model="formModel.deviceCode" placeholder="请输入设备ID" />
            </el-form-item>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="close">取消</el-button>
                <el-button :disabled="disabled" type="primary" @click="handleSumbit">
                    确定
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>
